﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Portal.Web
{
    public class BaseController : Controller
    {
        public BaseController()
        {
            ViewData["Result"] = null;
        }
        private static readonly Type CurrentCategorysKey = typeof(ICategory);
        private static readonly Type CurrentCultureKey = typeof(String);
        /// <summary>
        ///  创建一个返回实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T CreateViewData<T>() where T : BaseViewData, new()
        {
            var viewData = new T();
            return viewData;
        }
        /// <summary>
        /// 判断当前请求是否是AJAX
        /// </summary>
        public virtual bool IsAjaxRequest { get { return Request.IsAjaxRequest(); } }


        /// <summary>
        /// 获取系统分类列表
        /// </summary>
        public virtual IList<ICategory> Category
        {
            get
            {
                if (_category != null)
                    return _category;
                var list = HttpContext.Items[CurrentCategorysKey] as IList<ICategory>;
                if (list == null)
                {
                    list = CategoryCache.GetList();
                    HttpContext.Items[CurrentCategorysKey] = list;
                    _category = list;
                }
                return list;
            }
        }
        private IList<ICategory> _category;

        /// <summary>
        /// 返回结果
        /// </summary>
        public virtual IResult Result { get; set; }
        /// <summary>
        /// 当前请求上下文
        /// </summary>
        public virtual HttpContext HttpContextCurrent { get { return System.Web.HttpContext.Current; } }




        /// <summary>
        /// 系统语言环境
        /// </summary>
        public virtual string Culture
        {
            get
            {
                if (_culture != null)
                    return _culture;
                var culture = HttpContextCurrent.Items[CurrentCultureKey];
                if (culture == null)
                {
                    HttpContextBase contextWrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
                    RouteData routeData = RouteTable.Routes.GetRouteData(contextWrapper);
                    if (routeData != null)
                    {
                        routeData.Values.TryGetValue("culture", out culture);
                    }
                }
                _culture = culture != null ? culture.ToString() : "zh-cn";
                HttpContextCurrent.Items[CurrentCultureKey] = _culture;
                return _culture;
            }
        }
        private string _culture;

        /// <summary>
        /// 验证表示否通过验证
        /// </summary>
        public virtual void FormIsVaid()
        {
            if (!ModelState.IsValid)
            {
                var list = ModelState.GetEnumerator();
            }

        }
    }
}
